package org.realogic.cfm.persistence;

import java.util.HashMap;
import java.util.Map;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

/**
 * 数据源仓库<br>
 * 
 * 1:扫描系统所有DataSource和SqlSessionFactory
 * <p>
 * 2:记录系统所有DataSource的SqlSessionTemplate和SqlSessionTemplate
 * 
 */
public class KDataSourceRepository implements ApplicationContextAware {
	// 系统默认的数据源名称
	private String defaultDataSourceName;

	private Map<String, JdbcTemplate> jdbcTemplateMap = new HashMap<String, JdbcTemplate>();
	private JdbcTemplate defaultJdbcTemplate;

	private Map<String, SqlSessionTemplate> sqlSessionTemplateMap = new HashMap<String, SqlSessionTemplate>();
	private SqlSessionTemplate defaultSqlSessionTemplate;

	public KDataSourceRepository() {

	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Map<String, KDataSourceRegister> dataSourceRegisters = applicationContext.getBeansOfType(KDataSourceRegister.class);
		Map<String, SqlSessionFactoryBean> sqlSessionFactoryBeanMap = applicationContext.getBeansOfType(SqlSessionFactoryBean.class);
		SqlSessionFactoryBean tmpSqlSessionFactoryBean = null;

		for (KDataSourceRegister dataSourceRegister : dataSourceRegisters.values()) {
			SqlSessionTemplate tmpSqlSessionTemplate = null;
			JdbcTemplate tmpJdbcTemplate = null;
			// JdbcDaoSupport jdbcDaoSupport = null;
			// 创建JdbcTemplate对象
			// jdbcDaoSupport = new JdbcDaoSupport() {
			//
			// };
			// jdbcDaoSupport.setDataSource(dataSourceRegister.getDataSource());
			// jdbcTemplateMap.put(dataSourceRegister.getDataSourceName(),
			// jdbcDaoSupport.getJdbcTemplate());

			tmpJdbcTemplate = new JdbcTemplate(dataSourceRegister.getDataSource());
			jdbcTemplateMap.put(dataSourceRegister.getDataSourceName(), tmpJdbcTemplate);

			// 创建MyBatis SqlSessionTemplate对象
			if (dataSourceRegister.getSqlSessionFactoryName() != null) {
				try {
					tmpSqlSessionFactoryBean = sqlSessionFactoryBeanMap.get("&" + dataSourceRegister.getSqlSessionFactoryName());
					if (tmpSqlSessionFactoryBean != null) {
						tmpSqlSessionTemplate = new SqlSessionTemplate(tmpSqlSessionFactoryBean.getObject());
						sqlSessionTemplateMap.put(dataSourceRegister.getDataSourceName(), tmpSqlSessionTemplate);
					}
				} catch (Exception e) {
					throw new IllegalStateException(e.getLocalizedMessage());
				}
			}
			if (dataSourceRegister.isAsDefault()) {
				defaultDataSourceName = dataSourceRegister.getDataSourceName();
				// this.defaultJdbcTemplate = jdbcDaoSupport.getJdbcTemplate();
				this.defaultJdbcTemplate = tmpJdbcTemplate;
				this.defaultSqlSessionTemplate = tmpSqlSessionTemplate;
			}
		}

		if (defaultDataSourceName == null) {
			throw new IllegalStateException("请在数据源仓库中注册一个默认的数据源...");
		}
	}

	public String getDefaultDataSourceName() {
		return defaultDataSourceName;
	}

	public Map<String, JdbcTemplate> getJdbcTemplateMap() {
		return jdbcTemplateMap;
	}

	public JdbcTemplate getDefaultJdbcTemplate() {
		return defaultJdbcTemplate;
	}

	public Map<String, SqlSessionTemplate> getSqlSessionTemplateMap() {
		return sqlSessionTemplateMap;
	}

	public SqlSessionTemplate getDefaultSqlSessionTemplate() {
		return defaultSqlSessionTemplate;
	}

	public NamedParameterJdbcTemplate getDefaultNamedParameterJdbcTemplate() {
		return new NamedParameterJdbcTemplate(this.getDefaultJdbcTemplate());
	}

	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate(String dataSourceName) {
		if (this.jdbcTemplateMap.containsKey(dataSourceName)) {
			return new NamedParameterJdbcTemplate(jdbcTemplateMap.get(dataSourceName));
		} else {
			return getDefaultNamedParameterJdbcTemplate();
		}
	}

}
